package com.jiantong.user.impl.manager;

import com.jiantong.common.util.MD5Util;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.*;
import com.jiantong.user.model.AppUserRole;
import com.jiantong.user.model.OrganizationUser;
import com.jiantong.user.model.User;
import com.jiantong.user.model.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by niufei on 2017/4/21.
 *
 */
@Component
public class StaffManager {

    private final UserDao userDao;

    private final UserProfileDao userProfileDao;

    private final AppUserRoleDao appUserRoleDao;

    private final OrganizationUserDao organizationUserDao;

    private final GovernmentUserDutyDao governmentUserDutyDao;

    private final static String DEFAULT_PASSWORD = "123456";

    @Autowired
    public StaffManager(UserDao userDao,
                        UserProfileDao userProfileDao,
                        AppUserRoleDao appUserRoleDao,
                        OrganizationUserDao organizationUserDao,
                        GovernmentUserDutyDao governmentUserDutyDao) {
        this.userDao = userDao;
        this.userProfileDao = userProfileDao;
        this.appUserRoleDao = appUserRoleDao;
        this.organizationUserDao = organizationUserDao;
        this.governmentUserDutyDao = governmentUserDutyDao;
    }

//    /**
//     * 政府端创建员工
//     * @param userName 用户名
//     * @param mobile 手机号
//     * @param departments 部门
//     * @param declareStations 申报点
//     * @param butcherFactories 屠宰场
//     * @param roles 角色
//     * @return 是否写入成功
//     */
//    @Transactional
//    public Boolean createGovStaff(String userName , String mobile , List<Map<String , Object>> departments , List<Map<String , Object>> declareStations , List<Map<String , Object>> butcherFactories , Map<String , Object> roles){
//        LoginUser loginUser = UserUtil.getCurrentUser();
//
//        //创建user对象
//        User user = createUser(mobile,roles);
//        userDao.create(user);
//
//        //创建userProfile对象
//        UserProfile userProfile = createUserProfile(userName,user,loginUser);
//        userProfileDao.create(userProfile);
//
//        //创建organizationUser对象
//        OrganizationUser organizationUser = createOrganizationUser(userName,user , loginUser,roles);
//        organizationUserDao.create(organizationUser);
//
//        //创建departmentUser对象
//        List<DepartmentUser> departmentUserList = createDepartmentUser(userName,user , departments);
//        departmentUserDao.creates(departmentUserList);
//
//        //创建GovernmentUserDuty对象
//        List<GovernmentUserDuty> governmentUserDuties = createGovernmentUserDuty(userName,user,declareStations,butcherFactories);
//        governmentUserDutyDao.creates(governmentUserDuties);
//
//
//        return user.getId() != null;
//    }
//
//    /**
//     * 政府端跟新员工
//     * @param userId 用户id
//     * @param userName 用户name
//     * @param departments 部门
//     * @param declareStations 申报点
//     * @param butcherFactories 屠宰点
//     * @param roles 角色
//     * @return 是否写入成功
//     */
//    @Transactional
//    public Boolean updateGovStaff(Long userId ,String userName , List<Map<String , Object>> departments , List<Map<String , Object>> declareStations , List<Map<String , Object>> butcherFactories , Map<String , Object> roles){
//        Boolean flag ;
//        LoginUser loginUser = UserUtil.getCurrentUser();
//
//        //创建需要跟新的用户对象
//        User user = updateUser(userId,roles);
//        flag = userDao.update(user);
//
//        //创建userProfile对象
//        UserProfile userProfile = createUserProfile(userName,user,loginUser);
//        userProfileDao.updateByUserId(userProfile);
//
//        //创建organizationUser对象
//        OrganizationUser organizationUser = createOrganizationUser(userName,user,loginUser,roles);
//        organizationUserDao.updateByUserId(organizationUser);
//
//        //创建departmentUser对象
//        List<DepartmentUser> departmentUserList = createDepartmentUser(userName,user , departments);
//        //先根据userId 删除信息 再写入新的信息
//        departmentUserDao.deleteByUserId(userId);
//        departmentUserDao.creates(departmentUserList);
//
//        //创建GovernmentUserDuty对象
//        List<GovernmentUserDuty> governmentUserDuties = createGovernmentUserDuty(userName,user,declareStations,butcherFactories);
//        //先根据userId 删除信息 在写入新的信息
//        governmentUserDutyDao.deleteByUserId(userId);
//        governmentUserDutyDao.creates(governmentUserDuties);
//
//        return flag;
//    }

    /**
     * 创建养殖端的员工 屠宰端
     * @param userName 用户名
     * @param mobile 手机号
     * @param appRole 角色
     * @return 是否写入成功
     */
    @Transactional
    public Boolean createNGOStaff(String userName , String mobile , Integer appRole){
        LoginUser loginUser = UserUtil.getCurrentUser();

        //创建用户对象
        User user = createUser(mobile);
        userDao.create(user);

        //创建userProfile对象
        UserProfile userProfile = createUserProfile(userName,user ,loginUser);
        userProfileDao.create(userProfile);

        //创建organizationUser对象
        OrganizationUser organizationUser = createOrganizationUser(userName,user,loginUser);
        organizationUserDao.create(organizationUser);

        //创建userAppRole对象
        AppUserRole appUserRole = createAppUserRole(user,appRole);
        appUserRoleDao.create(appUserRole);

        return user.getId() != null;
    }

    /**
     * 更新员工--养殖端 屠宰端
     * @param userId 用户id
     * @param userName 用户名
     * @param appRole 角色
     * @return 是否写入成功
     */
    @Transactional
    public Boolean updateNGOStaff(Long userId , String userName , Integer appRole , Integer status){
        Boolean flag ;
        LoginUser loginUser = UserUtil.getCurrentUser();

         //创建用户对象
        User user = updateUser(userId, status);
        flag = userDao.update(user);

        //创建userProfile对象
        UserProfile userProfile = createUserProfile(userName,user ,loginUser);
        userProfileDao.updateByUserId(userProfile);

        //创建organizationUser对象
        OrganizationUser organizationUser = createOrganizationUser(userName,user,loginUser);
        organizationUserDao.updateByUserId(organizationUser);

        AppUserRole appUserRole = createAppUserRole(user,appRole);
        appUserRoleDao.updateByUserId(appUserRole);

        return flag;
    }

//    /**
//     * 创建政府端人员 与 申报点和屠宰点的 关联信息的对象  list
//     * @param user 用户
//     * @param declareStations 申报点
//     * @param butcherFactories 屠宰场
//     * @return List<GovernmentUserDuty>
//     */
//    private List<GovernmentUserDuty> createGovernmentUserDuty(String userName ,User user,List<Map<String , Object>> declareStations, List<Map<String , Object>> butcherFactories) {
//        List<GovernmentUserDuty> governmentUserDuties = new ArrayList<>();
//        GovernmentUserDuty governmentUserDuty ;
//        for(Map<String , Object> map : declareStations){
//            governmentUserDuty = new GovernmentUserDuty();
//            governmentUserDuty.setUserId(user.getId());
//            governmentUserDuty.setUserName(userName);
//            governmentUserDuty.setFactoryOrPointId(new Long((Integer) map.get("id")));
//            governmentUserDuty.setFactoryOrPointName((String)map.get("name"));
//            governmentUserDuty.setFactoryOrPointType(1);
//            governmentUserDuties.add(governmentUserDuty);
//        }
//        for(Map<String , Object> map : butcherFactories){
//            governmentUserDuty = new GovernmentUserDuty();
//            governmentUserDuty.setUserId(user.getId());
//            governmentUserDuty.setUserName(userName);
//            governmentUserDuty.setFactoryOrPointId(new Long((Integer) map.get("id")));
//            governmentUserDuty.setFactoryOrPointName((String)map.get("name"));
//            governmentUserDuty.setFactoryOrPointType(2);
//            governmentUserDuties.add(governmentUserDuty);
//        }
//        return governmentUserDuties;
//    }
//
//    /**
//     * 创建部门用户
//     * @param user 用户对象
//     * @return List<DepartmentUser>
//     */
//    private List<DepartmentUser> createDepartmentUser(String userName ,User user,List<Map<String , Object>> departments) {
//        List<DepartmentUser> departmentUserList = new ArrayList<>();
//        DepartmentUser departmentUser;
//        for (Map<String , Object> map:departments) {
//            departmentUser = new DepartmentUser();
//            departmentUser.setUserId(user.getId());
//            departmentUser.setUserName(userName);
//            departmentUser.setDepartmentId(new Long((Integer) map.get("id")));
//            departmentUser.setDepartmentName((String) map.get("name"));
//            departmentUser.setDepartmentType((Integer) map.get("type"));
//            departmentUser.setStatus(user.getStatus());
//            departmentUserList.add(departmentUser);
//        }
//        return departmentUserList;
//    }

    /**
     *
     * 创建组织用户对象
     * @param user 用户对象
     * @return OrganizationUser
     */
    private OrganizationUser createOrganizationUser(String userName ,User user,LoginUser loginUser) {
        OrganizationUser organizationUser = new OrganizationUser();
        organizationUser.setUserId(user.getId());
        organizationUser.setUserName(userName);
        organizationUser.setOrganizationId(loginUser.getOrganizationId());
        organizationUser.setOrganizationName(loginUser.getOrganizationName());
        organizationUser.setOrganizationType(loginUser.getOrganizationType());
        return organizationUser;
    }

    /**
     * 创建userProfile对象
     * @param user 用户对象
     * @return UserProfile
     */
    private UserProfile createUserProfile(String userName , User user ,LoginUser loginUser) {
        UserProfile userProfile = new UserProfile();
        userProfile.setUserId(user.getId());
        userProfile.setRealName(userName);
        userProfile.setGender(1);
        userProfile.setProvince(loginUser.getUserProfile().getProvince());
        userProfile.setProvinceId(loginUser.getUserProfile().getProvinceId());
        userProfile.setCity(loginUser.getUserProfile().getCity());
        userProfile.setCityId(loginUser.getUserProfile().getCityId());
        userProfile.setRegion(loginUser.getUserProfile().getRegion());
        userProfile.setRegionId(loginUser.getUserProfile().getRegionId());
        userProfile.setStreet(loginUser.getUserProfile().getStreet());
        userProfile.setStreetId(loginUser.getUserProfile().getStreetId());
        userProfile.setVillage(loginUser.getUserProfile().getVillage());
        userProfile.setVillageId(loginUser.getUserProfile().getVillageId());
        return userProfile;
    }

    /**
     * 创建用户对象
     * @param mobile 手机号
     * @return User
     */
    private User createUser(String mobile) {
        User user = new User();
        user.setName(mobile);
        user.setMobile(mobile);
        user.setPassword(MD5Util.encrypt(DEFAULT_PASSWORD));
        user.setStatus(1);
        user.setType(2);
        user.setAreaLevel(5);
        return user;
    }

    /**
     * 创建用户对象
     * @param userId 用户id
     * @param status 状态
     * @return User
     */
    private User updateUser(Long userId,Integer status) {
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        return user;
    }

    /**
     * 创建用户角色的对象
     * @param user 用户
     * @param appRole 角色
     * @return AppUserRole
     */
    private AppUserRole createAppUserRole(User user, Integer appRole) {
        AppUserRole appUserRole = new AppUserRole();
        appUserRole.setUserId(user.getId());
        appUserRole.setAppRole(appRole);
        appUserRole.setStatus(1);

        return appUserRole;
    }
}
